<?php

namespace app\common\model;

use app\model\BelongsTo;
use think\Model;

/**
 * em_order_trip
 * @property integer $id (主键)
 * @property integer $order_id 订单ID
 * @property integer $start_company_id 始发站
 * @property integer $company_id 所属办事处ID
 * @property integer $end_company_id 终点站
 * @property string $photos 拍照验车
 * @property integer $inspect_status 拍照验车结果 1:验车无异议，2:验车上报异常
 * @property string $inspect_remark 验车备注
 * @property integer $status 0.验车中 1.到提回待发 2.到配板发车 3.发车了(发车后，创建下一站的记录)
 * @property string $create_time
 * @property string $update_time
 * @property integer $board_id 板车ID
 */
class OrderTrip extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';
    
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'em_order_trip';
    
    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';
    
    const START_STATUS = [
        '拍照验车', '提回待发', '入库可发', '已发出', '已完结'
    ];
    
    const TRANSFER_START_STATUS = [
        '中转接车', '中转待发', '中转可发', '已发出', '已完结'
    ];
    
    const TERMINAL_START_STATUS = [
       0=>'待验车',1=>'待车辆入库',2=>"待配板发车", 3 => '到站待提', 4 => '客户已取车'

    ];
    
    const EXCEPTION_STATUS = [
        '无异常', '异常:未处理', '异常:车辆退回', '异常:继续发车'
    ];
    
    public function getPhotosAttr($value)
    {
        return array_map(function ($item) {
            $item['url'] = getImageUrl($item['path']);
            return $item;
        }, json_decode(empty($value) ? '[]' : $value, true));
    }
    
    /**
     * 订单信息
     * @return BelongsTo|\think\model\relation\BelongsTo|void
     */
    public function orderinfo()
    {
        return $this->belongsTo('Order', 'order_id')->bind([
            'car_number',
            'car_color',
            'car_brand',
            'address',
            'end_address',
            'car_shelf_num'
        ]);
    }
    
    public function depts()
    {
        return $this->hasOne(Dept::class, 'id', 'dept_id')->select(['id','name']);
    }

    public function startDept()
    {
        return $this->belongsTo('Dept', 'start_dept_id')->bind(['start_dept_name' => 'name']);
    }

    public function nowDept()
    {
        return $this->belongsTo('Dept', 'dept_id')->bind(['dept_name' => 'name']);
    }

    public function endDept()
    {
        return $this->belongsTo('Dept', 'end_dept_id')->bind(['end_dept_name' => 'name']);
    }
}
